
import SnowMan from "../Matter/SnowMan";
import GlobalEvent from "../../global/GlobalEvent";
import SnowBall from "../Matter/SnowBall";
import GamePlayView from "./GamePlayView";
import MatterManager from "../../managers/MatterManager";
import GameView from "./GameView";
import SnowBall2 from "./SnowBall2";
import SnowMan2 from "./SnowMan2";

export default class GameControl2 extends Laya.Script {


    /** @prop {name:snowManPrefab,tips:"雪人",type:Prefab}*/
    snowManPrefab: Laya.Prefab;

    /** @prop {name:snowBallPrefab,tips:"雪球",type:Prefab}*/
    snowBallPrefab: Laya.Prefab;


    realOwner: GameView;

    snowManRig: Laya.RigidBody;

    isMove = false;
    speed = 5;
    itemAngle = 0;

    snowManCollider: Laya.BoxCollider;
    snowBallCollider: Laya.CircleCollider;
    snowManControl: SnowMan2;

    gameItemRig: Laya.RigidBody;

    onEnable(): void {
        this.realOwner = this.owner as GameView;

        this.snowManControl = this.realOwner.snowMan.getComponent(SnowMan2) as SnowMan2;

        this.snowManCollider = this.realOwner.snowMan.getComponent(Laya.BoxCollider) as Laya.BoxCollider;
        this.snowBallCollider = this.realOwner.snowMan.getComponent(Laya.CircleCollider) as Laya.CircleCollider;

        this.snowManRig = this.realOwner.snowMan.getComponent(Laya.RigidBody) as Laya.RigidBody;

        GlobalEvent.instance.on('wheel_start', this, () => {
            // this.isMove = true;
            // this.addSnowBall();
            // this.startGrownUp();
            this.snowManControl.addSnowBall();
            this.snowManControl.startMove();

        })

        GlobalEvent.instance.on("wheel_change", this, (angle) => {
            // this.itemAngle = angle;
            this.snowManControl.changeDirection(angle);
        })

        GlobalEvent.instance.on('wheel_stop', this, () => {
            // this.isMove = false;
            // this.stopGrownUp();
            // this.emit();
            this.snowManControl.stopMove();

        })

    }

    // grownUp() {
    //     if (this.snowBallCollider) {
    //         this.snowBallCollider.radius++;
    //         this.snowBallCollider.y = (100 - this.snowBallCollider.radius * 2) / 2;
    //     }
    // }

    onUpdate() {
        // if (this.isMove) {
        //     let v = this.getVectorByAngle(this.itemAngle, this.speed);
        //     this.snowManRig.setVelocity(v);
        //     this.realOwner.snowMan.rotation = -(this.itemAngle - 2 * Math.PI) / Math.PI * 180;
        // } else {
        //     this.snowManRig.setVelocity({ x: 0, y: 0 });
        // }
    }

    // startGrownUp() {
    //     Laya.timer.frameLoop(5, this, this.grownUp);
    // }

    // stopGrownUp() {
    //     Laya.timer.clear(this, this.grownUp);
    // }


    // addSnowBall() {
    //     // this.snowBallCollider
    //     let snowBallCollider = new Laya.CircleCollider();
    //     snowBallCollider.radius = 25;
    //     snowBallCollider.x = 150;
    //     snowBallCollider.y = 25;
    //     this.realOwner.snowMan.addComponentIntance(snowBallCollider)
    //     this.snowBallCollider = snowBallCollider;
    // }

    // emit() {
    //     let x = this.realOwner.snowMan.x
    //     let y = this.realOwner.snowMan.y;
    //     let radius = this.snowBallCollider.radius;
    //     let distance = 100 + radius;
    //     let v = this.getVectorByAngle(this.itemAngle, distance)
    //     let point = {
    //         x: x+v.x,
    //         y: y+v.y
    //     };

        

    //     this.snowBallCollider.enabled = false;
    //     this.snowBallCollider.destroy();
    //     this.snowBallCollider = null;
    //     this.realOwner.snowMan._destroyComponent(this.snowBallCollider)


    //     let ball = this.snowBallPrefab.create() as Laya.Sprite;
    //     // this.realOwner.snowMan.addChild(ball);
    //     ball.width = radius * 2;
    //     ball.height = radius * 2;
    //     ball.pivot(radius /2, radius /2);
    //     ball.pos(point.x, point.y)
    //     this.realOwner.addChild(ball);
    //     let ballRig = ball.getComponent(Laya.RigidBody) as Laya.RigidBody;
    //     let script = ball.getComponent(SnowBall2) as SnowBall2;
    //     script.setRadius(radius);
    //     let ballV = this.getVectorByAngle(this.itemAngle, this.speed*2);


    //     ballRig.setVelocity(ballV);
    // }


    getVectorByAngle(angle: number, speed: number) {
        let x = Math.cos(angle) * speed;
        let y = -Math.sin(angle) * speed;
        return {
            x, y
        }
    }

    onDisable(): void {
    }
}